package cn.information.view.baseinfo.baseinfo;

import cn.information.bean.Product;
import cn.information.dao.ProductDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

public class ProductAddputUI extends JFrame {
    JScrollPane jScrollPane=new JScrollPane();
    AbstractTableModel tableModel;
    Vector<Vector> vector = new Vector();
    ProductDao dao=new ProductDao();

    JTextField txtID =new JTextField(3);
    JTextField txtGysid=new JTextField(3);
    JTextField txtNum =new JTextField(10);
    JTextField txtType=new JTextField(10);
    JTextField txtSL=new JTextField(10);
    JTextField txtPrice=new JTextField(10);

    public ProductAddputUI(String productAdd) {
        setTitle("商品信息");
        setBounds(300, 300, 400, 400);
        setLayout(new BorderLayout());

        JPanel panelCenter = new JPanel();
        panelCenter.setLayout(new GridLayout(6, 2));

        // 添加所有输入框
        panelCenter.add(new JLabel("商品ID："));
        panelCenter.add(txtID);
        panelCenter.add(new JLabel("供应商ID："));
        panelCenter.add(txtGysid);

        panelCenter.add(new JLabel("商品单价："));
        panelCenter.add(txtPrice);
        panelCenter.add(new JLabel("商品数量："));
        panelCenter.add(txtNum);
        panelCenter.add(new JLabel("商品保质期："));
        panelCenter.add(txtSL);
        panelCenter.add(new JLabel("商品类别："));
        panelCenter.add(txtType);

        add(panelCenter, BorderLayout.NORTH);

        JButton btnAdd = new JButton("添加");
        btnAdd.addActionListener(e -> addData(productAdd));
        JPanel panelSouth = new JPanel();
        panelSouth.add(btnAdd);
        add(panelSouth, BorderLayout.SOUTH);


        // 初始化表格并加载数据
        createTable(productAdd);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
    }



    private void addData(String productAdd) {
        Product product = new Product();
        product.setSpid(Integer.parseInt(txtID.getText()));
        product.setGysid(Integer.parseInt(txtGysid.getText()));
        product.setSpname(productAdd);
        product.setSpprice(Double.parseDouble(txtPrice.getText()));
        product.setSpnumber(Integer.parseInt(txtNum.getText()));
        product.setSL(txtSL.getText());
        product.setSptype(txtType.getText());
        try {
            if(dao.addProduct(product)){
                JOptionPane.showMessageDialog(this, "添加成功");
                select();
            } else {
                JOptionPane.showMessageDialog(this, "添加失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    private void createTable(String productAdd) {
        String[] columnNames = {"商品ID", "供应商ID", "商品名称", "商品单价", "商品数量", "商品保质期"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);

        List<Product> list = dao.findByType(productAdd);
        for (Product product : list) {
            Vector<Object> v = new Vector<>();
            v.add(product.getSpid());
            v.add(product.getGysid());
            v.add(productAdd);
            v.add(product.getSpprice());
            v.add(product.getSpnumber());
            v.add(product.getSL());
            vector.add(v);
        }

        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex < vector.size()) {
                    Vector v = vector.get(rowIndex);
                    return v.get(columnIndex);
                } else {
                    return null;
                }
            }
        };
        table.setModel(tableModel);
    }
    private void select() {
        vector.clear();
        java.util.List<Product> list = dao.findAll();
        for (Product product : list) {
            Vector v = new Vector();
            v.add(product.getSpid());
            v.add(product.getGysid());
            v.add(product.getSpname());
            v.add(product.getSpprice());
            v.add(product.getSpnumber());
            v.add(product.getSL());
            v.add(product.getSptype());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }
}